
import { OperateName } from "./WebDBDefine"

export class WebWhereData {
    //列名
    keys: string[] = []
    //条件名称
    names: string[] = []
    and: boolean = true;
    data: any[] = []
    constructor(operate: string, param: string) {
        switch (operate) {
            case OperateName.select:
                this.parseHead(param)
                break;
            case OperateName.insert:
                if (param) {
                    if (param.indexOf('@') >= 0) {
                        param = param.replace(/@/g, '')
                        let list = param.split(',')
                        for (let index = 0; index < list.length; index++) {
                            const element = list[index];
                            this.keys[index] = element.trim();
                        }
                    }
                }
                break;
            case OperateName.update:
                this.parseHead(param)
                break;
            case OperateName.delete:
                this.parseHead(param)
                break;
        }
    }

    parseHead(param: string) {
        if (param) {//有可能没有条件
            if (param.indexOf('and') >= 0) {
                this.and = true;
                let list = param.split('and')
                for (let index = 0; index < list.length; index++) {
                    const element = list[index];
                    this.parseData(element)
                }
            } else if (param.indexOf('or') >= 0) {
                this.and = false;
                let list = param.split('or')
                for (let index = 0; index < list.length; index++) {
                    const element = list[index];
                    this.parseData(element)
                }
            } else {
                this.parseData(param)
            }
        }
    }

    parseData(element: string) {
        if (element.indexOf('?') >= 0) {
            let temp = element.split('=')
            this.keys.push(temp[0].trim())
        } else if (element.indexOf('@') >= 0) {
            let s = element.replace(/@/g, '')
            // LogUtils.log(' s ', s)
            let temp = s.split('=')
            this.keys.push(temp[0].trim())
            this.names.push(temp[1].trim())
        } else {
            let temp = element.split('=')
            this.keys.push(temp[0].trim())
            this.data.push(temp[1].trim())
            //let sqlStr = "SELECT * FROM sqlite_master where type ='table' and name ='" + this.tbName + "'"

        }
    }
}
